package com.oromwin.repos.socket.response.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposConfig;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.Common.ReposResult;
import com.oromwin.repos.common.helper.IOHelper;
import com.oromwin.repos.socket.response.AbstractResponse;

public class ReadFileResponse extends AbstractResponse {
    private Log log = Logger.getLog(ReadFileResponse.class);
    private File FilePath = null;
    // other
    private long FileBytes = 0L;
    private File LocalPath = null;

    public ReadFileResponse() {
    }

    public ReadFileResponse(ReposResult result, String description, File filePath, File localPath) {
        Result = result;
        Description = description;
        FilePath = filePath;
        LocalPath = localPath;
    }

    @Override
    public void receive(DataInputStream dis) throws ReposException {
        try {
            Result = ReposResult.valueOf(IOHelper.readUTF8(dis));
            Description = IOHelper.readUTF8(dis);
            LocalPath = new File(IOHelper.readUTF8(dis));
            FileBytes = dis.readLong();
            // checkFreeSpaceExists(); // FIXME: 클라이언트 단에서도 용량체크가 필요함. 
            int off = 0;
            int len = ReposConfig.getInstance().getReposSocketIOBuffSize();
            int read = 0;
            byte[] b = new byte[len];
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(LocalPath));
            while (read < FileBytes) {
                int readBytes = dis.read(b, off, len);
                read += readBytes;
                bos.write(b, off, readBytes);
            }
            bos.flush();
            bos.close();
        } catch (IOException e) {
            throw ReposException.create(log, e);
        }
    }

    @Override
    public void send(DataOutputStream dos) throws ReposException {
        try {
            IOHelper.writeUTF8(dos, Result.toString());
            IOHelper.writeUTF8(dos, Description);
            IOHelper.writeUTF8(dos, LocalPath.toString());
            FileBytes = FilePath.length();
            dos.writeLong(FileBytes);
            int off = 0;
            int len = ReposConfig.getInstance().getReposSocketIOBuffSize();
            int read = 0;
            byte[] b = new byte[len];
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FilePath));
            while ((read = bis.read(b, off, len)) > 0) {
                dos.write(b, off, read);
            }
            dos.flush();
        } catch (IOException e) {
            throw ReposException.create(log, e);
        }
    }
    
    public File getFilePath() {
        return FilePath;
    }
}
